﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using StarBotDotNet;

namespace StarBotDotNet
{
    public static class LocationAnalyzer
    {

        public static Point FindLocationForUnit(UnitType available, ProxyBot bot, UnitDictionary currentUnits)
        {
            foreach (UnitData un in currentUnits.GetAllUnits())
            {
                if (un.Type.Center)
                {
                    if (un.OwnedByPlayer)
                    {
                        int initialX = un.Unit.X;
                        int initialY = un.Unit.Y;
                        //int checkDistance = 20;
                        int checkDistance = 9;
                        for (int x = -checkDistance; x < checkDistance; x++)
                        {
                            for (int y = -checkDistance; y < checkDistance; y++)
                            {
                                if (LocationValid(x + initialX, y + initialY, available.TileWidth, available.TileHeight, bot.Map, currentUnits, null))
                                {

                                    return new Point(x + initialX, y + initialY);

                                }
                            }
                        }

                    }
                }
            }

            return Point.Empty;

        }

        public static bool LocationContansUnits(int x, int y, int withd, int height, UnitDictionary currentUnits, UnitData builder)
        {
            Rectangle locRect = new Rectangle(x, y, withd, height);
            foreach (UnitData unit in currentUnits.GetAllUnits())
            {
                if (unit == builder)
                {
                    continue;
                }

                Rectangle unitRect = new Rectangle(unit.Unit.X, unit.Unit.Y, unit.Type.TileWidth, unit.Type.TileHeight);
                if (locRect.IntersectsWith(unitRect))
                {
                    return true;
                }
            }

            return false;

        }

        public static bool LocationValid(int x, int y, int w, int h, Map map, UnitDictionary currentUnits, UnitData builder)
        {
            if (CanBuild(x, y, w, h, map))
            {
                if (!LocationContansUnits(x, y, w, h, currentUnits, builder))
                {
                    return true;
                }
            }
            //TODO:Check walkable
            return false;

        }

        public static bool CanBuild(int x_pos, int y_pos, int w, int h, Map map)
        {
            for (int col = 0; col < w; col++)
            {
                for (int row = 0; row < h; row++)
                {
                    if (!map.isBuildable(col + x_pos, row + y_pos))
                    {
                        return false;
                    }

                }
            }
            return true;
        }
    }
}
